// RecommendFragment.java
package com.example.mode.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.mode.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class RecommendFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private int[] imageResIds = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
            R.drawable.image7,
            R.drawable.image8,
            R.drawable.image9,
            R.drawable.image10
    };

    private ImageView complimentImage;
    private TextView complimentText;
    private Random random = new Random();
    private int currentImageIndex = -1; // 记录当前显示的图片索引

    public RecommendFragment() {
        // Required empty public constructor
    }

    public static RecommendFragment newInstance(String param1, String param2) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);

        // 显示日期和时间
        TextView dateTimeText = view.findViewById(R.id.date_time_text);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String currentDateAndTime = sdf.format(new Date());
        dateTimeText.setText(currentDateAndTime);

        complimentImage = view.findViewById(R.id.compliment_image);
        complimentText = view.findViewById(R.id.compliment_text);
        EditText diaryEditText = view.findViewById(R.id.diary_edit_text);
        Button refreshButton = view.findViewById(R.id.refresh_button);

        // 初始化显示第一张图片和夸人文字
        updateImageAndText();

        // 设置刷新按钮的点击监听器
        refreshButton.setOnClickListener(v -> updateImageAndText());

        return view;
    }

    private void updateImageAndText() {
        // 更新图片（确保不重复显示同一张图片）
        updateImage();

        // 更新夸人文字
        updateComplimentText();
    }

    private void updateImage() {
        int previousIndex = currentImageIndex;
        // 生成不同的随机索引
        do {
            currentImageIndex = random.nextInt(imageResIds.length);
        } while (previousIndex == currentImageIndex && imageResIds.length > 1);

        complimentImage.setImageResource(imageResIds[currentImageIndex]);

        // 添加图片切换动画
        complimentImage.animate()
                .alpha(0f)
                .setDuration(200)
                .withEndAction(() -> {
                    complimentImage.setImageResource(imageResIds[currentImageIndex]);
                    complimentImage.animate()
                            .alpha(1f)
                            .setDuration(200)
                            .start();
                })
                .start();
    }

    private void updateComplimentText() {
        String[] compliments = getResources().getStringArray(R.array.compliments);
        int textIndex = random.nextInt(compliments.length);
        complimentText.setText(compliments[textIndex]);

        // 添加文字切换动画
        complimentText.animate()
                .alpha(0f)
                .setDuration(200)
                .withEndAction(() -> {
                    complimentText.setText(compliments[textIndex]);
                    complimentText.animate()
                            .alpha(1f)
                            .setDuration(200)
                            .start();
                })
                .start();
    }
}